package server.alarmThreads;

import org.apache.log4j.Logger;

import server.config.ServerConfig;

public class PocsagUeberwacher implements Runnable {

	/**
	 * 
	 */
	private static Logger logger = Logger.getLogger(PocsagUeberwacher.class);

	private static long lastaktive = System.currentTimeMillis();
	
	private static long startCount = 1;

	public PocsagUeberwacher() {

	}

	public static void pocsagKeepAlive() {

		lastaktive = System.currentTimeMillis();
	}

	@Override
	public void run() {

		if (ServerConfig.getInstance().isPocsagueberwacher()) {

			logger.info("Pocsagueberwacheraktive!");

			while (true) {

				try {

					if (lastaktive + (10 * 60 * 1000) < System.currentTimeMillis()) {

						logger.error("POCSAG POC32 neugestartet!!! Startcount: " + startCount);
						
						startCount++;
						
						// Processkillen

						@SuppressWarnings("unused")
						Process proc = Runtime.getRuntime().exec("taskkill /F /IM poc32.exe", null);

						Thread.sleep(3000);

						// Process starten
						@SuppressWarnings("unused")
						Process proc2 = Runtime.getRuntime().exec("poc32.exe", null);

						// Timeout zurücksetzten
						lastaktive = (long) (System.currentTimeMillis() + (60 * 6.5 * 1000));
					} else {

						Thread.sleep(5000);
					}

				} catch (Exception e) {
					logger.error("", e);
				}

			}
		}
	}
}
